﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCM.Logic;
using DataAccess.DbHelper;
using SubSonic.Query;
using DataModel;
using Common;

namespace Web.Accounting
{
    public partial class SupplierPO : System.Web.UI.Page
    {
        List<string> _order = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            //_order = new List<string>();
            //_order.Add(View_SupplierPO.SendTimeColumn);
            if (Page.IsPostBack == false)
            {
                ddlStatus.DataSource = M_StatusCode.All().ToList();
                ddlStatus.DataTextField = M_StatusCode.StatusNameColumn;
                ddlStatus.DataValueField = M_StatusCode.StatusCodeColumn;
                ddlStatus.DataBind();

            }

        }

        private void BindDataGrid()
        {
            List<ConditionHelper.SqlqueryCondition> wheres = InquiryCondition();
            View_SupplierPOBll.GetInstence().BindGrid(GridResult, GridResult.PageIndex + 1, GridResult.PageSize, wheres, _order);
        }


        private List<ConditionHelper.SqlqueryCondition> InquiryCondition()
        {
            var list = new List<ConditionHelper.SqlqueryCondition>();
            list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.SupplierCodeColumn, Comparison.Equals, Context.User.Identity.Name));
            //订货单号
            if (!string.IsNullOrEmpty(TextBoxOrderNo.Text))
            {
                list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.OrderNoColumn, Comparison.Like, "%" + StringHelper.FilterSql(TextBoxOrderNo.Text) + "%"));
            }
            //店铺名称
            /*
            if (!string.IsNullOrEmpty(TextBoxShopName.Text))
            {
                list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.SupplierCodeColumn, Comparison.Like, "%" + StringHelper.FilterSql(TextBoxShopName.Text) + "%"));
            }*/

            //单据状态
            if (!string.IsNullOrEmpty(ddlStatus.SelectedValue))
            {
                list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.StatusCodeColumn, Comparison.Equals, ConvertHelper.Cint0(ddlStatus.SelectedValue)));
            }
            //付款单号
            if (!string.IsNullOrEmpty(TextBoxReconciliationNo.Text))
            {
                list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.ReconciliationNoColumn, Comparison.Like, "%" + StringHelper.FilterSql(TextBoxReconciliationNo.Text) + "%"));
            }
            //起始时间
            if (!string.IsNullOrEmpty(DatePickerStartDate.Text))
            {
                list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.SendTimeColumn, Comparison.GreaterOrEquals, StringHelper.FilterSql(DatePickerStartDate.Text)));
                //终止时间
                if (!string.IsNullOrEmpty(DatePickerEndDate.Text))
                {
                    list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_SupplierPO.SendTimeColumn, Comparison.LessOrEquals, StringHelper.FilterSql(DatePickerEndDate.Text)));
                }
            }
            return list;
        }

        protected void GridResult_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            //处理排序
            _order = null;
            _order = new List<string>();
            if (e.SortField.Length > 0)
            {
                //判断是升序还是降序
                if (StringHelper.Upper(e.SortDirection) == "DESC")
                {
                    _order.Add(e.SortField + " desc");
                }
                else
                {
                    _order.Add(e.SortField + " asc");
                }
                //判断排序关键字是否为主键，否的话加上主键
                if (e.SortField.ToLower() != View_SupplierPO.OrderNoColumn.ToLower())
                {
                    _order.Add(View_SupplierPO.OrderNoColumn + " desc");
                }
            }
            else
            {
                _order.Add(View_SupplierPO.OrderNoColumn + " desc");
            }

            GridResult.SortColumn = e.SortField;
            GridResult.SortColumnIndex = e.ColumnIndex;
            BindDataGrid();
        }

        protected void GridResult_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            GridResult.PageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }


    }
}
